package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.RemainingDAO;
import org.millioncall.yueyoga.admin.model.Remaining;

public class RemainingDAOImpl extends BaseDao implements RemainingDAO {

	@Override
	public Remaining findById(Integer id) {
		Remaining remaining = this.getPagableSqlSessionTemplate().selectOne(
				"findRemainingById", id);
		return remaining;
	}


	@Override
	public PaginatedList<Remaining> findInPage(int start, int limit) {
		try {
			PreparedQuery<Remaining> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllRemaining");
			PaginatedList<Remaining> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int save(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveRemaining", map);
	}

	@Override
	public void deleteByIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().insert("deleteRemaining", map);
		}
	}

	@Override
	public PaginatedList<Remaining> findInPage(int start, int limit,
			String[] paramNames, String[] paramValues) {
		try {
			PreparedQuery<Remaining> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllRemaining");
			PaginatedList<Remaining> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Remaining findyje(Integer memberId) {
		return this.getPagableSqlSessionTemplate().selectOne("findyje",
				memberId);
	}

	@Override
	public boolean updateRemainingById(Remaining remaining) {
		return this.getPagableSqlSessionTemplate().update(
				"updateRemainingById", remaining) > 0;
	}

}
